package stv5.handlers;

import stv5.Request;
import stv5.Response;
import stv5.abstracted.RequestHandler;
import us.danlib.templating.Template;
import us.danlib.templating.Templator;

public class StatusHandler extends RequestHandler {
	
	private double refreshTime = 0;
	private String title = "SeriesTracker v5", status = "Loading..."; // some default stuff
	private Templator template;
	
	public StatusHandler() {
		template = new Templator("template/settings.generic.tpl");
	}
	
	/**
	 * Set how long before it should refresh the page, in seconds
	 * 
	 * @param newTime If zero, it won't set a refresh
	 */
	public void setRefreshTime(double newTime) {
		refreshTime = newTime;
	}
	
	public void setStatus(String newStatus) {
		status = newStatus;
	}

	@Override
	protected boolean handle(Request request, Response response) {
		Template tpl = template.createTemplate("TITLE", title, "BODY", status);
		if (refreshTime > 0)
			tpl.addGlobalVar("REFRESH", "" + refreshTime);
		response.setContentType("text/html");
		response.setBody(tpl.render());
		return true;
	}

}
